package controller

import (
	"data-system/common"
	"data-system/models"
	"fmt"

	"github.com/gorilla/websocket"
)

type Handler interface {
	Handle() (any, error)
}

type BaseAction struct {
	Action  string               `json:"action"`
	Param   any                  `json:"param"`
	Channel chan models.XMessage `json:"channel"`
	Client  *websocket.Conn      `json:"client"`
}

type handler func(action string, channel chan models.XMessage, conn *websocket.Conn, param any) (Handler, error)

var handlers = map[string]handler{
	common.WsPageLogicAction: NewPageLogicAction,
}

func RouteHandler(channel chan models.XMessage, conn *websocket.Conn, req models.Req) (Handler, error) {
	if handleF, ok := handlers[req.Action]; ok {
		handle, err := handleF(req.Action, channel, conn, req.Param)
		if err != nil {
			return nil, err
		}
		return handle, nil
	} else {
		return nil, fmt.Errorf("%q没有对应处理器，请联系开发人员", req.Action)
	}
}
